﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DnPakExplorer.Forms;

namespace DnPakExplorer.Controls {
	public partial class ItemIconBox : UserControl {

		[Browsable(false)]
		public IUserItem Item { get; set; }

		[Category("功能")]
		public bool ShowItemInfoOverlay { get; set; }

		[Browsable(false)]
		public bool ItemPropertyChanged { 
			get{
				return _ItemPropertyChanged;
			}
			set { 
				_ItemPropertyChanged = value;
				if (mOverlay != null && mOverlay.Visible && Item != null && value) {
					mOverlay.Item = Item;
					_ItemPropertyChanged = false;
				}
			}
		}
		private bool _ItemPropertyChanged;

		private InfoOverlayForm mOverlay;

		public ItemIconBox() {
			InitializeComponent();
		}

		private void ItemIconBox_Paint(object sender, PaintEventArgs e) {
		//	if (e.ClipRectangle.IntersectsWith(Bounds)) {
				if (Item != null) {
					if (Item.iconId >= 0) {
						Util.DrawItemImage(Item.iconId, this.ClientRectangle, e.Graphics);
					}
					if (Item.rank >= 0 && Item.rank<=6) {
						Util.DrawItemBox(Item.rank, this.ClientRectangle, e.Graphics);
					}
					Util.DrawItemTags(Item, this.ClientRectangle, e.Graphics);
				}

				if (DesignMode) {
					e.Graphics.DrawRectangle(Pens.Black, ClientRectangle);
				}
		//	}
		}

		private void ItemIconBox_MouseMove(object sender, MouseEventArgs e) {
			if (ShowItemInfoOverlay && Item!=null) {
				mOverlay = InfoOverlayForm.GetOrCreateItemInfoForm();
				if (ClientRectangle.Contains(e.Location)) {
					if (mOverlay.Item != Item || ItemPropertyChanged) {
						mOverlay.Item = Item;
						ItemPropertyChanged = false;
					}
					mOverlay.Show();
					mOverlay.UpdateInfoFormPosition();
					Capture = true;
				} else {
					Capture = false;
					mOverlay.Hide();
				}
			}
		}

		private void ItemIconBox_MouseLeave(object sender, EventArgs e) {
			if (ShowItemInfoOverlay && Item!=null) {
				InfoOverlayForm overlay = InfoOverlayForm.GetOrCreateItemInfoForm();
				overlay.Hide();
			}
		}
	}
}
